/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runebound.ui.combate;

import java.awt.Color;
import runebound.combate.FaseCombate;
import runebound.combate.PlanCombate;
import runebound.manejadores.RBControladorCombate;
import runebound.manejadores.RBControladorJuego;
import runebound.manejadores.eventos.RBEventoInicioCombate;
import runebound.manejadores.eventos.combate.RBEventoCombatePlanificado;
import runebound.modelo.heores.Heroe;
import runebound.modelo.monstruos.Monstruo;
import runebound.random.Tirada;

/**
 *
 * @author Miguel (alakat@gmail.com)
 */
public class RBDuranteCombatePanel extends javax.swing.JPanel {

    private Heroe heroe;
    private Monstruo monstruo;
    private static final Color COLOR_DEFECTO_BOTONES = new Color(238, 238, 238);
    private static final Color COLOR_FASE_ACTIVA_BOTONES = Color.orange;

    /**
     * Creates new form RBDuranteCombatePanel
     */
    public RBDuranteCombatePanel() {
        initComponents();
        inicializarInterfaz();
        this.fisicoButton.setText("ATAC");
        this.fisicoButton.setSelected(true);
    }

    public void setCombate(Monstruo m, Heroe h) {
        this.monstruo = m;
        this.heroe = h;
        this.fisicoEnfrentadaLabel.setText(h.getValorFisico() + " vs " + m.getValorFisico());
        this.distanciaEnfrentadaLabel.setText(h.getValorMente() + " vs " + m.getValorMente());
        this.magiaEnfrentadaLabel.setText(h.getValorMagia() + " vs " + m.getValorMagia());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToggleButton2 = new javax.swing.JToggleButton();
        jLabel1 = new javax.swing.JLabel();
        distanciaButton = new javax.swing.JToggleButton();
        fisicoButton = new javax.swing.JToggleButton();
        magiaButton = new javax.swing.JToggleButton();
        distanciaEnfrentadaLabel = new javax.swing.JLabel();
        fisicoEnfrentadaLabel = new javax.swing.JLabel();
        magiaEnfrentadaLabel = new javax.swing.JLabel();
        huirButton = new javax.swing.JButton();
        combatirButton = new javax.swing.JButton();
        d1Label = new javax.swing.JLabel();
        d2Label = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tituloExito = new javax.swing.JLabel();
        faseCombateTitulo = new javax.swing.JLabel();

        jToggleButton2.setText("jToggleButton2");

        setMaximumSize(new java.awt.Dimension(400, 150));
        setPreferredSize(new java.awt.Dimension(400, 150));
        setSize(new java.awt.Dimension(400, 150));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("<html> Plan de </br>ataque</html>");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(19, 21, 55, -1));

        distanciaButton.setText("DEF");
        distanciaButton.setActionCommand("ATAC");
        distanciaButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        distanciaButton.setDoubleBuffered(true);
        distanciaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distanciaButtonActionPerformed(evt);
            }
        });
        add(distanciaButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 21, 61, 32));

        fisicoButton.setText("DEF");
        fisicoButton.setActionCommand("ATAC");
        fisicoButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        fisicoButton.setDoubleBuffered(true);
        fisicoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fisicoButtonActionPerformed(evt);
            }
        });
        add(fisicoButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(167, 21, 61, 32));

        magiaButton.setText("DEF");
        magiaButton.setActionCommand("ATAC");
        magiaButton.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        magiaButton.setDoubleBuffered(true);
        magiaButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                magiaButtonActionPerformed(evt);
            }
        });
        add(magiaButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(234, 21, 61, 32));

        distanciaEnfrentadaLabel.setFont(new java.awt.Font("Lucida Grande", 0, 8)); // NOI18N
        distanciaEnfrentadaLabel.setText("15 VS 12");
        add(distanciaEnfrentadaLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 59, -1, -1));

        fisicoEnfrentadaLabel.setFont(new java.awt.Font("Lucida Grande", 0, 8)); // NOI18N
        fisicoEnfrentadaLabel.setText("15 VS 12");
        add(fisicoEnfrentadaLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(188, 60, -1, -1));

        magiaEnfrentadaLabel.setFont(new java.awt.Font("Lucida Grande", 0, 8)); // NOI18N
        magiaEnfrentadaLabel.setText("15 VS 12");
        add(magiaEnfrentadaLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 60, -1, 10));

        huirButton.setText("Huir");
        huirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                huirButtonActionPerformed(evt);
            }
        });
        add(huirButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(301, 6, 90, -1));

        combatirButton.setText("Combatir");
        combatirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combatirButtonActionPerformed(evt);
            }
        });
        add(combatirButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(301, 41, 90, -1));

        d1Label.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        d1Label.setText("0");
        add(d1Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 100, -1, -1));

        d2Label.setFont(new java.awt.Font("Lucida Grande", 1, 24)); // NOI18N
        d2Label.setText("0");
        add(d2Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 100, -1, -1));

        jLabel2.setText("Tiradas:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 111, -1, -1));

        tituloExito.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        tituloExito.setForeground(new java.awt.Color(204, 0, 51));
        tituloExito.setText("Falla!");
        add(tituloExito, new org.netbeans.lib.awtextra.AbsoluteConstraints(177, 106, -1, -1));

        faseCombateTitulo.setText("Disparo");
        add(faseCombateTitulo, new org.netbeans.lib.awtextra.AbsoluteConstraints(58, 78, 170, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void distanciaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distanciaButtonActionPerformed
        // TODO add your handling code here:
        this.magiaButton.setSelected(false);
        this.fisicoButton.setSelected(false);
        this.magiaButton.setText("DEF");
        this.fisicoButton.setText("DEF");
        this.distanciaButton.setText("ATAC");
    }//GEN-LAST:event_distanciaButtonActionPerformed

    private void fisicoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fisicoButtonActionPerformed

        this.magiaButton.setSelected(false);
        this.distanciaButton.setSelected(false);
        this.magiaButton.setText("DEF");
        this.distanciaButton.setText("DEF");
        this.fisicoButton.setText("ATAC");
    }//GEN-LAST:event_fisicoButtonActionPerformed

    private void magiaButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_magiaButtonActionPerformed
        // TODO add your handling code here:
        this.distanciaButton.setSelected(false);
        this.fisicoButton.setSelected(false);
        this.distanciaButton.setText("DEF");
        this.fisicoButton.setText("DEF");
        this.magiaButton.setText("ATAC");
    }//GEN-LAST:event_magiaButtonActionPerformed

    private void combatirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combatirButtonActionPerformed
        // TODO add your handling code here:
        PlanCombate planCombate = new PlanCombate(
                this.distanciaButton.isSelected(),
                this.fisicoButton.isSelected(),
                this.magiaButton.isSelected(), false);
        RBEventoCombatePlanificado evento = new RBEventoCombatePlanificado(planCombate);
        RBControladorJuego.getInstance().recibirEvento(evento);
        this.distanciaButton.setEnabled(false);
        this.fisicoButton.setEnabled(false);
        this.magiaButton.setEnabled(false);
        this.combatirButton.setEnabled(false);
        this.huirButton.setEnabled(false);
    }//GEN-LAST:event_combatirButtonActionPerformed

    private void huirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_huirButtonActionPerformed
        // TODO add your handling code here:
        PlanCombate planCombate = new PlanCombate(
                false,
                false,
                false, true);
        RBEventoCombatePlanificado evento = new RBEventoCombatePlanificado(planCombate);
        RBControladorJuego.getInstance().recibirEvento(evento);
        this.distanciaButton.setEnabled(false);
        this.fisicoButton.setEnabled(false);
        this.magiaButton.setEnabled(false);
        this.combatirButton.setEnabled(false);
        this.huirButton.setEnabled(false);
    }//GEN-LAST:event_huirButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton combatirButton;
    private javax.swing.JLabel d1Label;
    private javax.swing.JLabel d2Label;
    private javax.swing.JToggleButton distanciaButton;
    private javax.swing.JLabel distanciaEnfrentadaLabel;
    private javax.swing.JLabel faseCombateTitulo;
    private javax.swing.JToggleButton fisicoButton;
    private javax.swing.JLabel fisicoEnfrentadaLabel;
    private javax.swing.JButton huirButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton magiaButton;
    private javax.swing.JLabel magiaEnfrentadaLabel;
    private javax.swing.JLabel tituloExito;
    // End of variables declaration//GEN-END:variables

    public void exitoTirada(Tirada t, boolean defensa) {
        this.d1Label.setText("" + t.getD1());
        this.d2Label.setText("" + t.getD2());
        if (defensa) {
            this.tituloExito.setText("DEFENSA!");
        } else {
            this.tituloExito.setText("Ataca!!");
        }
    }

    public void falloTirada(Tirada t, boolean defensa) {
        this.d1Label.setText("" + t.getD1());
        this.d2Label.setText("" + t.getD2());
        if (defensa) {
            this.tituloExito.setText("Heroe golpeado!");
        } else {
            this.tituloExito.setText("miss!");
        }

    }

    /**
     * Activa una nueva ronda de combate
     */
    public void nuevaRondaCombate() {
        inicializarInterfaz();
        this.distanciaButton.setEnabled(true);
        this.fisicoButton.setEnabled(true);
        this.magiaButton.setEnabled(true);
        this.combatirButton.setEnabled(true);
        this.huirButton.setEnabled(true);
        this.distanciaButton.setBackground(COLOR_DEFECTO_BOTONES);
        this.fisicoButton.setBackground(COLOR_DEFECTO_BOTONES);
        this.magiaButton.setBackground(COLOR_DEFECTO_BOTONES);
    }

    private void inicializarInterfaz() {
        this.d1Label.setText("");
        this.d2Label.setText("");
        this.tituloExito.setText("");
    }

    /**
     * Asigna una nueva fase dentro de la ronda de combate
     *
     * @param fc Fase
     */
    public void setFaseCombate(FaseCombate fc) {
        inicializarInterfaz();
        switch (fc) {
            case DISPARO:
                this.distanciaButton.setBackground(COLOR_FASE_ACTIVA_BOTONES);
                this.fisicoButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.magiaButton.setBackground(COLOR_DEFECTO_BOTONES);
                break;
            case HUIR:
                this.distanciaButton.setBackground(COLOR_FASE_ACTIVA_BOTONES);
                this.fisicoButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.magiaButton.setBackground(COLOR_DEFECTO_BOTONES);
                break;
            case FISICO:
                this.distanciaButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.fisicoButton.setBackground(COLOR_FASE_ACTIVA_BOTONES);
                this.magiaButton.setBackground(COLOR_DEFECTO_BOTONES);
                break;
            case MAGIA:
                this.distanciaButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.fisicoButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.magiaButton.setBackground(COLOR_FASE_ACTIVA_BOTONES);
                break;
            case ESPERA:
                this.distanciaButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.fisicoButton.setBackground(COLOR_DEFECTO_BOTONES);
                this.magiaButton.setBackground(COLOR_DEFECTO_BOTONES);
                break;

        }
    }

    /**
     * Muestra una retirada a tiempo
     * @param t 
     */
    public void exitoHuida(Tirada t) {
        this.d1Label.setText("" + t.getD1());
        this.d2Label.setText("" + t.getD2());

        this.tituloExito.setText("Huyo!");

    }

    public void fallohuida(Tirada t) {
        this.d1Label.setText("" + t.getD1());
        this.d2Label.setText("" + t.getD2());

        this.tituloExito.setText("NO huyes!");
    }
}
